package shopping.model;

import shopping.commons.ModelObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: mazhao
 * Date: 2010/10/31
 * Time: 17:13:43
 * To change this template use File | Settings | File Templates.
 */
public class ShoppingCartData extends ModelObject {

    /**  shopping cart data (product id, product count) */
    private final Map<Integer, Integer> items = new HashMap<Integer, Integer>();

    /**
     * put product into shopping cart.
     * @param productId product id
     * @param count product count
     */
    public void putItem(Integer productId, Integer count) {
        if (items.containsKey(productId)) {
            Integer origCnt = items.get(productId);
            items.put(productId, origCnt + count);
        } else {
            items.put(productId, count);
        }
    }

    /**
     * how many product in your shopping cart
     * @param productId counting product id
     * @return count
     */
    public Integer countItem(Integer productId) {
        if (items.containsKey(productId)) {
            return items.get(productId);
        } else {
            return 0;
        }
    }

    /**
     * get all the products and count in shopping cart
     * @return  products and count
     */
    public final Map<Integer, Integer> getItems() {
        return items;
    }
}
